package com.platform.modules.mall.activity.type;


import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.service.MallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.List;

public abstract class ActivityTypeAbstract implements ActivityTypeInterface {

    protected List<MallGoodsToActivityEntity> entityList;

    @Autowired
    protected MallGoodsService mallGoodsService;

    @Override
    public void setEntity(List<MallGoodsToActivityEntity> entityList) {
        this.entityList =entityList;
    }

    @Override
    public void delEntity(MallGoodsToActivityEntity entity) {
        Iterator<MallGoodsToActivityEntity> iterator = entityList.iterator();
        while (iterator.hasNext()){
            MallGoodsToActivityEntity goods = iterator.next();
            if (goods.getId().equals(entity.getId())){
                iterator.remove();
            }
        }
    }

    /**
     * 减扣商品库存，后续还需要操作，没有库存不直接抛出异常，需要操作下一个商品
     * @param goodsId
     * @param number
     * @return
     */
    @Override
    public boolean delGoodsNum(String goodsId,Integer number) {
        return  mallGoodsService.lambdaUpdate().setSql("goods_number=goods_number-" + number)
                .eq(MallGoodsEntity::getId, goodsId)
                .eq(MallGoodsEntity::getIsOnSale,1)
                .eq(MallGoodsEntity::getIsDelete,1)
                .ge(MallGoodsEntity::getGoodsNumber, number).update();
    }
}
